<template>
  <div style="padding: 0;">
    <card style="margin-top: 10px;">
      <div slot="content" style="padding: 10px;">
        <div class="warning">
          {{ $t('rechargeTips') }}
        </div>
        <div>
          <number-input
            :value="cash"
            @input="cashInput"
            class="cash-input"
            type="int"
            unit=""
            :placeholder="$t('pleashInputCashAmount')">
          </number-input>
        </div>

        <div style="margin: 10px 0;">
          <checker
            v-model="selectedCashIndex"
            radio-required
            default-item-class="select-item"
            selected-item-class="select-item-selected"
            @on-change="selectedCashChange"
          >
            <checker-item
              v-for="(price, index) of cashList"
              :value="index"
              :key="index">
              <div style="direction: ltr;">
                <span style="font-size: 16px;">¥</span> 
                <span style="font-size: 16px;">{{ price }}</span>
              </div>
            </checker-item>
          </checker>
        </div>

        <div class="pay-btn">
          <div @click="pay" :style="{'background-color': (isPaying ? '#F3BA6B' : '#eea236')}" style="text-align: center;padding: 8px 10px;color: #fff;margin: 0 auto;border-radius: 5px;">
            {{ isPaying ? $t('paying') : $t('confirmRecharge') }}
          </div>
        </div>
      </div>
    </card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  XInput,
  XButton,
  Checker, 
  CheckerItem,
} from "vux"
import Card from '@/components/Card'
import NumberInput from '@/components/NumberInput'
import NumberKeyboard from '@/components/NumberKeyboard'
import { rechargeAPI } from '@/api/pay'

const SCHEME_URL = 'weixin://dl/business/?appid=wx5094228fa43e85a2&path=pages/user/index/index&env_version=release'

export default {
  data() {
    return {
      cash: "",
      selectedCashIndex: null,
      cashList: ["50", "100", "300", "500", "1000", "2000"],
      isPaying: false
    }
  },
  computed: {
    ...mapGetters([
      'userInfo',
    ])
  },
  methods: {
    pay() {

      if (this.isPaying) {
        return
      }

      this.isPaying = true

      const cash = +this.cash
      if (!cash) {
        this.$vux.toast.text(this.$t("pleashInputCashAmount"))

        this.isPaying = false

        return 
      }

      const openid = this.userInfo.openid
      rechargeAPI({
        openid, 
        amount: cash
      }).then(({ data }) => {
        if (data.out_trade_no) {
          this.goToPay(
            data,
            openid,
            () => {
              this.isPaying = false

              this.$vux.confirm.show({
                title: "",
                content: this.$t("isGotoMiniprogramAfterPaySuccess"),
                cancelText: this.$t("cancel"),
                confirmText: this.$t("confirm"),
                onConfirm: () => {

                  this.$router.replace({ name: 'rechargeHome' })
                  
                  // 返回小程序userIndex页面
                  window.location.href = SCHEME_URL
                },
                onCancel: () => {
                  this.$router.replace({ name: 'rechargeHome' })
                }
              })
            },
            (err) => {
              this.isPaying = false
            }
          )

          return
        }

        this.isPaying = false
      }).catch(err => {
        this.isPaying = false
      })
    },
    cashInput(value) {
      this.cash = value
    },
    selectedCashChange(index) {

      this.cash = this.cashList[index] 
    }
  },
  components: {
    Card,
    XInput,
    XButton,
    Checker, 
    CheckerItem,
    NumberInput,
    NumberKeyboard
  }
}
</script>

<style lang="scss">
.warning {
  color: #fff;
  font-size: .9rem;
  text-align: center;
  background-color:#eea236;
  margin-bottom: 20px;
  padding: 10px;
  line-height: 2;
  border-radius: 5px;;
}

  .select-item {
    width: 31%;
    height: 60px;
    line-height: 60px;
    text-align: center;
    /* border-radius: 3px; */
    /* border: 1px solid #ccc; */
    background-color: #f7f7f7;
    margin: 8px 1%;
    font-size: .8rem;
  }

  .select-item-selected {
    background: rgba(243, 205, 152, 0.61);
  }

  .cash-input  {
    .weui-input {
      font-size: 20px;
      text-align: center !important;
    }
  }

  .van-number-keyboard--custom {
    .weui-cell {
      padding: 10px;
    }
  }

</style>